package objects;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Timer;
import java.util.TimerTask;

import modules.configuration.GameConf;
import modules.gameCommunication.GameServerThread;

import common.GameServer;
import common.LoadingData;
import common.SQLMgr;

public class Account {

    private long _guid, _subscribeEnd, _uniqueID;
    private byte _gmLevel;
    private String _pseudo, _answer, uniqueID = null, a = null;
    private Timer _t;

    public Account(long guid, String pseudo, byte gmLvl, String answer, long subEnd) {
        _uniqueID = guid;
        _guid = guid;
        _pseudo = pseudo;
        _gmLevel = gmLvl;
        _answer = answer;
        _subscribeEnd = subEnd;
        _t = new Timer();
        _t.schedule(new TimerTask() {

            @Override
            public void run() {
                GameServerThread.delWaitingAccount(_guid);
            }
        }, 5 * 60 * 1000);//5minutes between add in Waiting list and deleting
    }

    public Account() {
    }

    public String getGuidString() {
        a = Long.toString(getGuid());
        return this.a;
    }

    public long getGuid() {
        return this._uniqueID;
    }

    public String getPseudo() {
        return this._pseudo;
    }

    public boolean isValidAnswer(String ans) {
        return ans.equals(this._answer);
    }

    public byte getGmLevel() {
        return this._gmLevel;
    }

    public boolean isSubscriber() {
        return this._subscribeEnd > System.currentTimeMillis();
    }

    public void setUniqueID(String pUniqueID) {
        this.uniqueID = pUniqueID;
    }

    public String getUniqueID() {
        return this.uniqueID;
    }

    public synchronized ResultSet getCharList(long guid) {
        Statement stat = SQLMgr.createStat();
        ResultSet result = SQLMgr.query(stat, "SELECT * FROM characters WHERE accountID = " + guid + ";");

        //SQLMgr.freeSQLStat(stat);

        return result;
    }

    public synchronized String[] getCharData(long guid, long charID) // TODO : Return a Collection for closing the result and statement
    {
        String[] charData = new String[73];

        Statement stat = SQLMgr.createStat();
        ResultSet result = SQLMgr.query(stat, "SELECT * FROM characters WHERE accountID = " + guid + " AND uniqueID = " + charID + ";");

        try {
            result.next();

            charData[0] = result.getString("uniqueID");
            charData[1] = result.getString("pseudo");
            charData[2] = result.getString("accountID");
            charData[3] = result.getString("sex");
            charData[4] = result.getString("class");
            charData[5] = result.getString("gfxid");
            charData[6] = result.getString("color1");
            charData[7] = result.getString("color2");
            charData[8] = result.getString("color3");
            charData[9] = result.getString("level");
            charData[10] = result.getString("isDead");
            charData[11] = result.getString("isMerchant");
            charData[12] = result.getString("inventory");
            charData[13] = result.getString("exp");
            charData[14] = result.getString("kamas");
            charData[15] = result.getString("caractPoints");
            charData[16] = result.getString("spellsPoints");
            charData[17] = result.getString("vitality");
            charData[18] = result.getString("energy");
            charData[19] = result.getString("strength");
            charData[20] = result.getString("wisdom");
            charData[21] = result.getString("agility");
            charData[22] = result.getString("luck");
            charData[23] = result.getString("intelligence");
            charData[24] = result.getString("mapID");
            charData[25] = result.getString("cellID");
            charData[26] = result.getString("spells");
            charData[27] = result.getString("pa");
            charData[28] = result.getString("pm");
            charData[29] = result.getString("alignment");
            charData[30] = result.getString("aLevel");
            charData[31] = result.getString("aGrade");
            charData[32] = result.getString("aHonor");
            charData[33] = result.getString("aDeshonor");
            charData[34] = result.getString("aStatut");
            charData[35] = result.getString("life");
            charData[36] = result.getString("po");
            charData[37] = result.getString("CreatureInvocable");
            charData[38] = result.getString("bonusDo");
            charData[39] = result.getString("bonusDoP");
            charData[40] = result.getString("bonusDoM");
            charData[41] = result.getString("bonusDoPer");
            charData[42] = result.getString("bonusTrap");
            charData[43] = result.getString("bonusTrapPer");
            charData[44] = result.getString("bonusHeal");
            charData[45] = result.getString("damageReturn");
            charData[46] = result.getString("bonusCC");
            charData[47] = result.getString("bonusEC");
            charData[48] = result.getString("missPa");
            charData[49] = result.getString("missPM");
            charData[50] = result.getString("resistNeutral");
            charData[51] = result.getString("resistNeutralPer");
            charData[52] = result.getString("resistEarth");
            charData[53] = result.getString("resistEarthPer");
            charData[54] = result.getString("resistFire");
            charData[55] = result.getString("resistFirePer");
            charData[56] = result.getString("resistWater");
            charData[57] = result.getString("resistWaterPer");
            charData[58] = result.getString("resistAir");
            charData[59] = result.getString("resistAirPer");
            charData[60] = result.getString("resistPvpNeutral");
            charData[61] = result.getString("resistPvpNeutralPer");
            charData[62] = result.getString("resistPvpEarth");
            charData[63] = result.getString("resistPvpEarthPer");
            charData[64] = result.getString("resistPvpFire");
            charData[65] = result.getString("resistPvpFirePer");
            charData[66] = result.getString("resistPvpWater");
            charData[67] = result.getString("resistPvpWaterPer");
            charData[68] = result.getString("resistPvpAir");
            charData[69] = result.getString("resistPvpAirPer");
            charData[70] = result.getString("channels");
            charData[71] = result.getString("items");
            charData[72] = result.getString("size");


        } catch (SQLException e) {
            e.printStackTrace();
        }

        SQLMgr.freeSQLQuery(result);
        SQLMgr.freeSQLStat(stat);

        return charData;
    }

    public synchronized void createChar(String[] pChar) {
        final String gfxid = pChar[1].concat(pChar[2]);

        if (SQLMgr.getSelfSQL() == null) {
            GameServer.writeLine("SQL Error : Self = null");
        }

        // Add the character in the DB
        Statement stat = SQLMgr.createStat();
        SQLMgr.queryU(stat, "INSERT INTO characters(`pseudo`, `accountID`, `sex`, `class`, `gfxid`, `color1`, `color2`, `color3`, `level`, `mapID`, `cellID`, `life`) "
                + "VALUES('" + pChar[0] + "', " + getGuid() + ", " + Integer.parseInt(pChar[2]) + ", " + Integer.parseInt(pChar[1]) + ", " + Integer.parseInt(gfxid) + ", " + Integer.parseInt(pChar[3]) + ", " + Integer.parseInt(pChar[4]) + ", " + Integer.parseInt(pChar[5]) + ", " + GameConf._initLevel + ", " + GameConf._startMap + ", " + GameConf._startCell + ", " + LoadingData.getRaceData().get(Integer.parseInt(pChar[1]))[2] + ");");

        // TODO: Update server_characters
        SQLMgr.freeSQLStat(stat);

    }

    public synchronized void delChar(String pChar) {

        if (SQLMgr.getSelfSQL() == null) {
            GameServer.writeLine("SQL Error : Self = null");
        }
        //FIXME: Can delete character from other account ? è_é
        // Delete the character
        Statement stat = SQLMgr.createStat();
        SQLMgr.queryU(stat, "DELETE FROM `characters` WHERE (`uniqueID`='" + pChar + "')");

        // TODO: Update server_characters
        SQLMgr.freeSQLStat(stat);

    }
}